package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.TrainDAO;
import com.zerosky.crm.po.Train;
import com.zerosky.crm.service.TrainService;
import com.zerosky.crm.util.PageInfo;

/**
 * Train业务逻辑实现类
 * 
 * @author 钟璟;
 * @since 2017-07-13
 *
 */
@Service(value="trainService")
public class TrainServiceImpl implements TrainService {
	@Autowired
	private TrainDAO TrainDAO; // Train数据接口

	/**
	 * Train列表
	 * 
	 * @author 钟璟;
	 * @since 2017-07-13
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Train> getTrainList(Map map) {
		PageInfo<Train> page = new PageInfo<Train>();
		int begin = (((int) map.get("page") - 1) * (int) map.get("pageSize"));

		// 写入数据
		map.put("pageNo", begin);
		List<Train> list = TrainDAO.getTrainList(map);
		page.setList(list);

		int count = TrainDAO.getTrainListCount(map);// 获取总记录
		page.setPageCount(count);
		return page;
	}

	/**
	 * 通过主键查找Train实体
	 * 
	 * @param id
	 *            Train主键
	 * @author 钟璟;
	 * @since 2017-07-13
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Train getTrainById(String id) {
		return TrainDAO.getTrainById(id);
	}

	/**
	 * 保存Train
	 * 
	 * @param Train
	 *            实体对象, operation 操作类型
	 * @author 钟璟;
	 * @since 2017-07-13
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void saveTrain(Train Train, String operation) {
		if ("新增".equalsIgnoreCase(operation)) {
			TrainDAO.addTrain(Train);
		} else if ("修改".equalsIgnoreCase(operation)) {
			TrainDAO.updateTrain(Train);
		}
	}

	/**
	 * 删除Train
	 * 
	 * @param idArray
	 *            Train主键数组
	 * @author 钟璟;
	 * @since 2017-07-13
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void deleteTrainById(String id) {
		TrainDAO.deleteTripById(id);
	}
}
